package scripts;

import api.CommonApi;
import api.LoginApi;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import utils.AssertUtil;
import utils.ResponseData;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;

public class TestLogin {
    private static final Logger log = LoggerFactory.getLogger(TestLogin.class);

    private final LoginApi loginApi = new LoginApi();
    private final CommonApi commonApi = new CommonApi();

    @DataProvider
    public Object[][] buildData() throws IOException {
        // 读取json文件
        String jsonStr = FileUtils.readFileToString(new File("./data/login.json"), "UTF-8");

        // 解析json数据，组装数据
        JSONArray jsonArray = JSONObject.parseArray(jsonStr);
        Object[][] testData = new Object[jsonArray.size()][];
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            testData[i] = new Object[]{jsonObject.get("username"), jsonObject.get("password"), jsonObject.get("code"),
                    jsonObject.get("statusCode"), jsonObject.get("status"), jsonObject.get("description")};
        }
        log.info("testData={}", Arrays.deepToString(testData));
        return testData;
    }

    @Test(dataProvider = "buildData")
    public void testLogin(String username, String password, String code, int statusCode,
                          int status, String description) throws Exception {
        // 测试数据
        log.info("params username={} password={} code={} statusCode={} status={} description={}",
                username, password, code, statusCode, status, description);

        // 获取图片验证码
        ResponseData verifyCode = commonApi.getVerifyCode(String.valueOf(new Random().nextDouble()));
        Assert.assertEquals(verifyCode.getCode(), 200);

        // 登录
        ResponseData responseData = loginApi.login(username, password, code);

        // 断言
//        Assert.assertEquals(login.getCode(), statusCode);
//        Assert.assertEquals(login.getBody().getIntValue("status"), status);
//        Assert.assertEquals(login.getBody().getString("description"), description);
        AssertUtil.Assert(responseData,statusCode,status,description);
    }


    @Test
    public void testLoginNoGetCode() throws Exception {
        // 测试数据
        String username = "admin";
        String password = "12345678";
        String code = "8888";

        // 登录
        ResponseData responseData = loginApi.login(username, password, code);

        // 断言
        Assert.assertEquals(responseData.getCode(), 200);
        Assert.assertEquals(responseData.getBody().getIntValue("status"), 100);
        Assert.assertEquals(responseData.getBody().getString("description"), "验证码错误");
//        AssertUtil.Assert(responseData,);
    }


    @Test
    public void testLoginSuccess() throws Exception {
        // 测试数据
        String username = "admin";
        String password = "12345678";
        String code = "8888";

        // 获取图片验证码
        ResponseData verifyCode = commonApi.getVerifyCode(String.valueOf(new Random().nextDouble()));
        Assert.assertEquals(verifyCode.getCode(), 200);

        // 登录
        ResponseData login = loginApi.login(username, password, code);

        // 断言
        Assert.assertEquals(login.getCode(), 200);
        Assert.assertEquals(login.getBody().getIntValue("status"), 200);
        Assert.assertEquals(login.getBody().getString("description"), "OK");
    }

}
